<?php
 
class Category extends Eloquent
{
	protected $fillable = array('name', 'parent_id');

	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'category';
	
    /**
     * Set timestamps off
     */
    public $timestamps = false;
 
	public function templates() {
        return $this->belongsToMany('template');
	}
	
	public function parent()
    {
        return $this->hasOne('category', 'id', 'parent_id');
    }

    public function children()
    {
        return $this->hasMany('category', 'parent_id', 'id');
    }
	
	public function isRoot()
	{
		return $this->parent_id == 0;
	}
	
	public function countChildren()
	{
		return count($this->children);
	}
	
}